package com.xea.action;

import java.io.IOException;
import java.lang.reflect.Method;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DispatcherServlet extends HttpServlet{

	private static String METHOD="m";
	
	private static String DEFAULTMETHOD="execute";
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -3988545829776169197L;
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("utf-8"); 
		String method = req.getParameter(METHOD);
		if(method == null)method =DEFAULTMETHOD;
		try {
			Method me = this.getClass().getMethod(method, new Class[]{
					HttpServletRequest.class, HttpServletResponse.class	
				});
			me.invoke(this, new Object[]{req, resp});
		} catch (Exception e) {
			throw new ServletException("执行方法" + method + "失败!", e);
		}
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}

}
